<?php
# 文件名称:language.model.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
class languageModel extends C_model {
	var $_lists   = array();
	
	var $_adminlists   = array();
	
    function languageModel()
    {
        parent::C_model();
    }
	//单个语言记录
	function getItem()
	{
		$SYS =&get_instance();
		//参数过滤
		$id = $SYS->request->getVar( 'cid', array(0), '', 'array' );
		$table = C_loader::getDTO('language');
		//数据
		$table->load($id[0]);
		return $table;
	}
	//翻译一个文件
	function saveTranslation($fileName, $key, $translation)
	{
		$origFile = $this->parseFile($fileName);
		$resultFile = array();
		foreach ($origFile as $line)
		{
			switch($line->key)
			{
				case '#':
				case '-':
					$resultFile[] = $line->value;
				break;
				default:
					$found = false;
					for($i=0;$i<count($key);$i++)
					{
						if( $line->key == $key[$i] )
						{
							//--Found it
							$resultFile[] = $line->key.'='.$translation[$i];
							$found = true;
							break;
						}
					}
					if(!$found)	$resultFile[] = $line->key.'='.$line->value;
					break;
			}//switch
		}//foreach
		$fileContents = implode("\n", $resultFile);
		if ( ! write_file($fileName, $fileContents) )
		{
			return false;
		}
		return true;
	}//function
	function parseFile($path)
	{
		if( ! file_exists($path))
		{
			return false;
		}
		//--Read the file
		$file = explode("\n", read_file($path));
		$parsed = array();
		foreach( $file as $line )
		{
			$line = trim($line);
			$translation = new StdClass();
			if( strpos($line, '#') === 0)
			{
				//--Comment line
				$translation->key = '#';
				$translation->value = $line;
				$parsed[]=$translation;
				continue;
			}
			$eqpos = strpos($line, '=');
			if( $eqpos )
			{
				//--Found a pair
				$key = substr($line, 0, $eqpos);
				$value = substr($line, $eqpos + 1);
				$translation->key = $key;
				$translation->value = $value;
				$parsed[]=$translation;
				continue;
			}
			continue;
			$translation->key = '-';
			$translation->value = $line;
			$parsed[]=$translation;
		}//foreach
		return $parsed;
	}//function
	//翻译的语言记录集
	function getLanguage()
	{
		$SYS =&get_instance();
		if($SYS->request->getvar('option')=="administrator")
		{
			$path=PATH_LANGUAGE;
		}else
		{
			$path=PATH_SITE.DS.'language';
		}
		$ret = array();
		//参数过滤
		$id = $SYS->request->getVar( 'cid', array(0), '', 'array' );
		//数据
		$query = 'SELECT *' .
				' FROM '.$this->_db->_table_prefix.'language'.
				' WHERE id='.$id[0]
				;
		$this->_db->setQuery($query);
		$lang = $this->_db->loadAssoc();
		$this->_lang = $lang;
		$lang = $lang['lang_code'];		
		$SYS->loader->helper('file');
		$filelists = $this->getFileLists($path.DS.$lang);
		for($i=0;$i<count($filelists);$i++)
		{
			$ret[$filelists[$i]->name] = $this->parseFile($filelists[$i]->filepath);
		}
		return $ret;
	}
	function getLang()
	{
		return $this->_lang;
	}
	//文件集
	function getFileLists($path)
	{
		//目录	
		$fso    =   @opendir($path);
		$result = 	array();
		$SYS =&get_instance();
		if($fso)
		{
			while($file=readdir($fso))
			{
				if($file!='.' && $file!='..')
				{		
					$filepath=$path.DS.$file;
					//处理文件
					if(is_file($filepath)) 
					{		
						if(preg_match("/([^?]*)\.ini/i",basename($filepath),$num))
						{
							$tmp = new stdClass();
							$tmp->filepath = $filepath;							
							$tmp->name = $num[1];
							$result[] = $tmp;
						}
					}
				}				
			}
		}
		@closedir($fso);
		return $result;
	}
	function getAdminItems()
	{
		return $this->getItems(2);
	}
	function getSiteItems()
	{
		return $this->getItems(1);
	}
	//语言记录集
	function getItems($type = 1)
	{
		if (!empty( $this->_data ))
		{
			return $this->_data;
		}
		$SYS =&get_instance();
		// 获取请求的数据
		$filter_order		= $SYS->request->getVar( "filter_order",		'a.title',		'post',	'cmd' );
		$filter_order_Dir	= $SYS->request->getVar( "filter_order_Dir",	'asc',	        'post',	'word' );
		//过滤查询
		$orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir;
		$where = ' WHERE lang_type='.$type;
		//数据集
		$query = 'SELECT *' .
				' FROM '.$this->_db->_table_prefix.'language'.' AS a '.
				$where.
				$orderby;
		$this->_data = $this->_getArrayList( $query );
		$this->_lists['order_Dir']	= $filter_order_Dir;
		$this->_lists['order']		= $filter_order;
		return $this->_data;
	}
	function getLists()
	{		
		return $this->_lists;
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>